package com.supply.dl.view;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.supply.dl.R;
import com.supply.dl.bean.PhoneInfoBean;
import com.supply.dl.utils.SystemUtil;

/**
 * Created by ${qianzhihe} on 2017/6/20.
 * 调用系统相机或者系统相册弹框界面
 */

public class MainMenuPopWindow extends PopupWindow {

    private View popView;
    private final int HEAD_ABLUME=1;// 相册
    private final int HEAD_CAREAM=2;// 相机
    private PopWindowClickCallBack mCallBack;

    public MainMenuPopWindow(final Activity context){
        //获取宽高
        PhoneInfoBean phoneInfoBean= SystemUtil.getPhoneWidthAndHeight(context);
        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        popView = layoutInflater.inflate(R.layout.main_menu,null);

        //设置父容器的宽高，以及margin
        LinearLayout parentLayout= (LinearLayout) popView.findViewById(R.id.main_menu_parentLayout);
        android.widget.LinearLayout.LayoutParams layoutParam= (android.widget.LinearLayout.LayoutParams) parentLayout.getLayoutParams();
        layoutParam.width= phoneInfoBean.getDisWidth()*4/5;
        layoutParam.height= phoneInfoBean.getDisHeight()*2/5;
        layoutParam.setMargins(30, 60, 30, 60);
        popView.setLayoutParams(layoutParam);

        //设置弹出的popview视图
        this.setContentView(popView);
        //设置弹出窗口的宽
        this.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        //设置高度  为layout的自适应高度
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        //设置pop可点击可见
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
//        // 实例化一个ColorDrawable颜色为半透明
//        ColorDrawable colorDrawable = new ColorDrawable(0000000000);
//        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener
//        this.setBackgroundDrawable(colorDrawable);

        // 设置MyPopWindow弹出窗体的背景
        this.setBackgroundDrawable(context.getResources().getDrawable(R.color.transparent_background));
        //设置透明度
        this.getBackground().setAlpha(230);

        // 设置pop弹出窗体动画效果
//        this.setAnimationStyle(R.style.PopWindowAnimation);
        // 获取SD卡路径
        LinearLayout ablumLayout = (LinearLayout)popView.findViewById(R.id.main_menu_ablumLayout);
        ablumLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setType(HEAD_ABLUME);
            }
        });

        LinearLayout caremaLayout = (LinearLayout)popView.findViewById(R.id.main_menu_caremaLayout);
        caremaLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setType(HEAD_CAREAM);
            }
        });


    }

    /**
     * 设置  pop 的弹出位置
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
//            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 5);
            this.showAtLocation(parent, Gravity.CENTER, 0, 0);
        } else {
            this.dismiss();
        }
    }

    public void setType(int mFlag){
        if (mCallBack!=null){
            mCallBack.getUserSeletedType(mFlag);
        }
        if (this.isShowing()){
            this.dismiss();
        }
    }

    public interface PopWindowClickCallBack{
        void getUserSeletedType(int flag);
    }

    public void setPopWindowClickCallBack(PopWindowClickCallBack callBack){
        this.mCallBack=callBack;
    }

}
